package org.iteam.xlz.core.service.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.ticket.TicketType;

public class TickettypeService extends BaseService<TicketType>{
	static TickettypeService instance;
	private TickettypeService(){}
	
	public static TickettypeService getInstance(){
		if(null==instance){
			instance=new TickettypeService();
		}
		return instance;
	}
	
	public String getTypeParent(String tid){
		String s="";
		if(null==tid){
			return "";
		}
		try {
			TicketType t=super.show("select r.* from ticketinfo l left join tickettype r on l.ttid=r.ttid where 1=1 and l.tid=?",tid);
			if(StringUtils.isNotBlank(t.getTypeparent())){
				s=t.getTypeparent();
			}
			return s;
		} catch (SQLException e) {
			e.printStackTrace();
			return s;
		}
	}
	
	public List<TicketTree> buildTicketTree(){
		try {
			List<TicketType> all=super.query("select * from tickettype");
			List<TicketTree> list=new ArrayList<TicketTree>();
			List<TicketTree> secondList=new ArrayList<TicketTree>();
			
			for(TicketType a:all){//设置一级区域
				if(null==a.getTypeparent()){
					TicketTree at=new TicketTree();
					at.setId(a.getTtid());
					at.setText(a.getTypename());
					list.add(at);
				}
			}
			
			for(TicketTree at:list){//设置二级区域
				List<TicketTree> children=new ArrayList<TicketTree>();
				for(TicketType a:all){
					if(at.getId().equals(a.getTypeparent())){
						TicketTree ati=new TicketTree();
						ati.setId(a.getTtid());
						ati.setText(a.getTypename());
						children.add(ati);
						secondList.add(ati);//这里面保存的全是二级区域
					}
				}
				at.setChildren(children);
			}
			
			for(TicketTree at:secondList){//设置三级区域
				List<TicketTree> children=new ArrayList<TicketTree>();
				for(TicketType a:all){
					if(at.getId().equals(a.getTypeparent())){
						TicketTree ati=new TicketTree();
						ati.setId(a.getTtid());
						ati.setText(a.getTypename());
						children.add(ati);
					}
				}
				at.setChildren(children);				
			}
			
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TicketTree>();
		}
	}
	
	public long counts(String typeparent){
		StringBuilder sql=new StringBuilder("select count(1) from tickettype where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(typeparent)){
			sql.append(" and typeparent=?");
			params.add(typeparent);
		}else{
			sql.append(" and typeparent is null");
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<TicketType> query(String typeparent,long page,long rows){
		StringBuilder sql=new StringBuilder("select * from tickettype where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(typeparent)){
			sql.append(" and typeparent=?");
			params.add(typeparent);
		}else{
			sql.append(" and typeparent is null");
		}
		sql.append(" limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TicketType>();
		}
	}
	
	public boolean create(TicketType model){
		SqlBuilder sql=SqlBuilder.createInsert("tickettype");
		sql.addField("ttid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("typename",model.getTypename());
		if(StringUtils.isNotBlank(model.getTypeparent())){
			sql.addField("typeparent",model.getTypeparent());
		}
		sql.addField("createtime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		sql.addField("status",model.getStatus());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(TicketType model){
		SqlBuilder sql=SqlBuilder.createUpdate("tickettype");
		sql.addField("typename",model.getTypename());
		sql.addField("status",model.getStatus());
		sql.addWhere("ttid",model.getTtid());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delete(String ttid){
		try {
			return super.updates("delete from tickettype where 1=1 and ttid=?",ttid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public TicketType findById(String ttid){
		try {
			return super.show("select * from tickettype where 1=1 and ttid=?",ttid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new TicketType();
		}
	}
	
	public class TicketTree{
		String id;
		String text;
		List<TicketTree> children;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public List<TicketTree> getChildren() {
			return children;
		}
		public void setChildren(List<TicketTree> children) {
			this.children = children;
		}
		
	}	
}
